<script setup lang="ts">
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { type User, useUserStore } from '@/stores/user.ts'

const router = useRouter()
const userStore = useUserStore()

onMounted(() => {
  const item = localStorage.getItem('user')
  if (item) {
    const user: User = JSON.parse(item)
    userStore.setUser(user)
  }

  // 检查登录状态
  const isLoggedIn = userStore.isLoggedIn

  if (!isLoggedIn) {
    // 未登录，跳转到登录页
    router.push('/login')
  }
  // 如果已登录，留在当前页面（主页）
})
</script>

<template>
  <RouterView />
</template>

<style scoped></style>
